package com.deshan.websys.code.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.deshan.websys.code.mapper.PmsStatisticsMapper;
import com.deshan.websys.code.model.PmsStatistics;
import com.deshan.websys.code.model.PmsVisitLog;
import com.deshan.websys.code.model.SmsHomeAdvertise;
import com.deshan.websys.code.model.vo.VisitLogVO;
import com.deshan.websys.code.service.PmsStatisticsService;
import com.deshan.websys.code.utils.UuidUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service

public class PmsStatisticsServiceImpl implements PmsStatisticsService {
    @Resource
    PmsStatisticsMapper pmsStatisticsMapper;
    @Override
    public int getPmsStatistics(String columnCode) {
        QueryWrapper<PmsStatistics> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("column_code", columnCode);
        int result=pmsStatisticsMapper.selectCount(queryWrapper);

        return result;
    }

    @Override
    public synchronized int saveOrUpdate(String columnCode) {
        int result=0;
        QueryWrapper<PmsStatistics> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("column_code", "columnCode");
        PmsStatistics desstatistics = pmsStatisticsMapper.selectOne(queryWrapper);
        if(desstatistics==null){
            PmsStatistics statistics =new PmsStatistics();
            statistics.setId(UuidUtils.shortUuid());
            statistics.setColumnCode(columnCode);
            statistics.setClickCount(1);
           result=  pmsStatisticsMapper.insert(statistics);
        }else {
            int count = desstatistics.getClickCount();
            desstatistics.setClickCount(count + 1);
            result = pmsStatisticsMapper.updateById(desstatistics);
        }
        return result;
    }

}
